<?php

namespace App\Http\Controllers;

use App\Models\CardSentence;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Validator;
use Illuminate\Http\Request;

class CardSentenceController extends Controller
{

  public function index(Request $request)
  {
    $sentences = CardSentence::where('title', 'like', '%'. $request->title .'%')
      ->paginate(10)
      ->toArray();
    return $this->success(0, '请求成功', $sentences);
  }

  public function store(Request $request)
  {
    $validator = Validator::make($request->all(), [
      'title' => 'required|between:2,190',
      'content' => 'required|between:2,190',
      'status' => 'required|boolean',
    ]);
    if ($validator->fails()) {
      return $this->failure(-1, $validator->errors()->first());
    }
    $data = $request->only(['title', 'content', 'status']);
    $sentence = CardSentence::create($data);
    return $this->success(0, '添加成功', $sentence);
  }

  public function getOne(Request $request)
  {
      if($request->all){
          $sentence = CardSentence::get();
      }else{
          $sentence = CardSentence::where('status', true)
              ->orderBy(DB::raw('rand()'))
              ->first();
      }
      return $this->success(0, 'OK', $sentence);
  }

  /*public function create(Request $request)
  {

  }*/

  public function update(Request $request)
  {
    $validator = Validator::make($request->all(), [
      'id' => 'required|exists:card_sentences',
      'title' => 'required|between:2,190'
    ]);
    if ($validator->fails()) {
      return $this->failure(-1, $validator->errors()->first());
    }
    $sentence = CardSentence::find($request->id);
    $sentence->title = $request->title;
    $sentence->content = $request->input('content');
    $sentence->status = $request->status;
    $sentence->save();
    return $this->success(0, '更新成功！', $sentence);
  }

  public function listpage(Request $request)
  {
    $sentences = CardSentence::all();
    return $this->success(0, '请求成功', $sentences);
  }

  public function destroy($id)
  {
    CardSentence::destroy($id);
    return $this->success(0, '删除成功！');
  }

    public function DeleteAll()
    {
        CardSentence::truncate();
        return $this->success(0, '清除成功！');
    }

  public function batchRemove(Request $request)
  {
    $ids = explode(',', $request->ids);
    foreach ($ids as $id) {
      CardSentence::destroy($id);
    }
    return $this->success(0, '删除成功！');
  }

    public function batchUpload(Request $request)
    {
        if ($request->file('cardSentence')->isValid()) {
            $path = $request->file('cardSentence')->store('cardSentence');
            $content = Storage::get($path);
            $n = 0;
            $data = [];
            if ($content) {
                $array = explode("\r\n", $content);
                $array = array_filter($array);
                $array_length = count($array);
                for ($i = 0; $i < $array_length; $i++) {
                    $tempdata = explode('----', trim($array[$i]));
                    $data[$n]['title'] = $tempdata[0];
                    $data[$n]['content'] = $tempdata[1];
                    $data[$n]['created_at'] = date('Y-m-d H:i:s');
                    $n++;
                }
                $count = count($data);
                if($count > 10000) {
                    $splick_count = floor(count($data)/1000);
                    if ($splick_count<= 0) $splick_count = 1;
                    $new_arr = array_chunk($data, $splick_count);
                    foreach ($new_arr as $item) {
                        CardSentence::insertOnDuplicateKey($item);
                    }
                } else {
                    CardSentence::insertOnDuplicateKey($data);
                }
                return $this->success(0, '上传成功！');
            }
            return $this->failure(-1, '文件内容为空！');
        }
    }

}
